import React, { useRef,useState } from 'react';


export const InputFocus: React.FC = () => {
    const [count, setCount] = useState(0);

    // useRef()只是在组件首次渲染的时候被创建
    // 如果组件时rerender的时候,不会重复创建ref对象;
    const oldValue = useRef<number>();
    
    // 自增
    const addFun = () => {
        setCount((pre) => pre + 1);
        oldValue.current = count;
    }

    return (
        <>
            <h1>新值是：{count}；旧值:{oldValue.current}</h1>
            <button type='button' onClick={addFun}>自增+1</button>
        </>
    )
}


